package org.hiucung.module_voice_setting

import android.widget.SeekBar
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import kotlinx.android.synthetic.main.activity_voice_setting.*
import org.hiucung.lib_base.base.BaseActivity
import org.hiucung.lib_base.base.adapter.CommonAdapter
import org.hiucung.lib_base.base.adapter.CommonViewHolder
import org.hiucung.lib_base.helper.ARouterHelper
import org.hiucung.lib_voice.manager.VoiceManager

@Route(path = ARouterHelper.PATH_VOICE_SETTING)
class VoiceSettingActivity : BaseActivity() {

    private val mList: ArrayList<String> = ArrayList()
    private var mTtsPeopleIndex:Array<out String>?= null

    override fun getLayoutId(): Int {
        return R.layout.activity_voice_setting
    }

    override fun getTitleText(): String {
        return getString(org.hiucung.lib_base.R.string.app_title_voice_setting)
    }

    override fun isShowBack(): Boolean {
        return true
    }

    override fun initView() {
        bar_voice_speed.progress = 5
        bar_voice_volume.progress = 5

        bar_voice_speed.max = 15
        bar_voice_volume.max = 15

        initData()

        initListener()

        initPeopleView()

        btn_test.setOnClickListener {
            VoiceManager.ttsStart("大家好，我是梅友基")
        }
    }

    private fun initData() {
        val mTtsPeople = resources.getStringArray(R.array.TTSPeople)
        mTtsPeopleIndex = resources.getStringArray(R.array.TTSPeopleIndex)

        mTtsPeople.forEach {
            mList.add(it)
        }
    }

    private fun initListener(){
        bar_voice_speed.setOnSeekBarChangeListener(object :SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(p0: SeekBar?, process: Int, p2: Boolean) {
                bar_voice_speed.progress = process
                VoiceManager.setVoiceSpeed(process.toString())
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {

            }

            override fun onStopTrackingTouch(p0: SeekBar?) {

            }

        })

        bar_voice_volume.setOnSeekBarChangeListener(object :SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(p0: SeekBar?, process: Int, p2: Boolean) {
                bar_voice_volume.progress = process
                VoiceManager.setVoiceVolume(process.toString())
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {
            }

            override fun onStopTrackingTouch(p0: SeekBar?) {
            }

        })
    }

    private fun initPeopleView(){
        rv_voice_people.layoutManager = LinearLayoutManager(this)
        rv_voice_people.addItemDecoration(DividerItemDecoration(this,DividerItemDecoration.VERTICAL))
        rv_voice_people.adapter = CommonAdapter(mList,object :CommonAdapter.OnBindDataListener<String>{
            override fun onBindViewHolder(
                model: String,
                viewHolder: CommonViewHolder,
                type: Int,
                position: Int
            ) {
                viewHolder.setText(R.id.mTvPeppleContent,model)
                viewHolder.itemView.setOnClickListener {
                    mTtsPeopleIndex?.let {
                        VoiceManager.setPeople(it[position])
                    }
                }
            }

            override fun getLayoutId(type: Int): Int {
                return R.layout.layout_tts_people_list
            }

        })
    }

}